package com.faithbj.webservice;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.faithbj.common.model.compare.AFMOfficalData;
import com.faithbj.common.model.compare.CivilOfficalData;
import com.faithbj.common.model.compare.CompareRecord;
import com.faithbj.common.model.compare.HPOfficalData;
import com.faithbj.common.model.compare.IBAOfficalData;
import com.faithbj.common.model.compare.PopulationOfficalData;
import com.faithbj.common.model.compare.SIOfficalData;
import com.faithbj.common.model.compare.TaxOfficalData;
import com.faithbj.common.utils.DateUtils;
import com.faithbj.service.CompareRecordService;
import com.faithbj.service.CompareService;
import com.faithbj.support.vo.AFMCompareResponse;
import com.faithbj.support.vo.AFMCompareResponse.AFMData;
import com.faithbj.support.vo.CivilCompareResponse;
import com.faithbj.support.vo.CivilCompareResponse.CivilData;
import com.faithbj.support.vo.CompareRequest;
import com.faithbj.support.vo.CompareResponse;
import com.faithbj.support.vo.HPCompareResponse;
import com.faithbj.support.vo.HPCompareResponse.HPData;
import com.faithbj.support.vo.IBACompareResponse;
import com.faithbj.support.vo.IBACompareResponse.IBAData;
import com.faithbj.support.vo.PSCompareResponse;
import com.faithbj.support.vo.PSCompareResponse.PSData;
import com.faithbj.support.vo.SICompareResponse;
import com.faithbj.support.vo.SICompareResponse.SIData;
import com.faithbj.support.vo.TaxCompareResponse;
import com.faithbj.support.vo.TaxCompareResponse.TaxData;

/**
 * webservice 实现类
 * 
 * @author chen liu
 */
@WebService(endpointInterface = "com.faithbj.webservice.WebServiceServer")
@Service("webServiceBean")
public class WebServiceServerImpl implements WebServiceServer {
    @Resource
    private CompareService compareService;

    @Resource
    private CompareRecordService compareRecordService;

    @Override
    public boolean sendRequest(List<CompareRequest> requestList) {
        for (CompareRequest request : requestList) {
            System.out.println(request);
        }
        return true;
    }

    @Override
    public boolean sendAFMResponse(List<AFMCompareResponse> responseList) {
        if (responseList != null && !responseList.isEmpty()) {
            for (CompareResponse response : responseList) {
                // 更新委办返回表
                AFMCompareResponse afmResponse = (AFMCompareResponse) response;
                List<AFMOfficalData> updateList = compareService.getAFMOfficalDataForUpdate(afmResponse.getBatchNum(),
                        afmResponse.getDepartmentNum(), afmResponse.getMainApplicantCardId());
                if (updateList != null) {
                    for (AFMOfficalData data : updateList) {
                        for (AFMData resData : afmResponse.getMemberDatas()) {
                            if (data.getApplicantCardNum().equals(resData.getApplicantCardNum())) {
                                BeanUtils.copyProperties(resData, data);
                                data.setIsQueryReturned(1);
                                data.setQueryReturnDt(DateUtils.getCurrentDate());
                                compareService.saveOrUpdateAFMOfficalData(data);
                                break;
                            }
                        }
                    }
                }
                // 更新比对记录表
                Map condition = new HashMap();
                condition.put("batchNum", afmResponse.getBatchNum());
                condition.put("type", afmResponse.getDepartmentNum());
                condition.put("mainApplicantIdNum", afmResponse.getMainApplicantCardId());
                List<CompareRecord> recordUpdateList = compareRecordService
                        .queryCompareRecordListByCondition(condition);
                if (recordUpdateList != null) {
                    for (CompareRecord record : recordUpdateList) {
                        record.setRecvNum(record.getRecvNum() + afmResponse.getMemberDatas().size());
                    }
                    compareRecordService.saveOrUpdate(recordUpdateList);
                }
            }
        }

        return true;
    }

    @Override
    public boolean sendCivilResponse(List<CivilCompareResponse> responseList) {
        if (responseList != null && !responseList.isEmpty()) {
            for (CompareResponse response : responseList) {
                // 更新委办返回表
                CivilCompareResponse civilResponse = (CivilCompareResponse) response;
                List<CivilOfficalData> updateList = compareService.getCivilOfficalDataForUpdate(
                        civilResponse.getBatchNum(), civilResponse.getDepartmentNum(),
                        civilResponse.getMainApplicantCardId());
                if (updateList != null) {
                    for (CivilOfficalData data : updateList) {
                        for (CivilData resData : civilResponse.getMemberDatas()) {
                            if (data.getApplicantCardNum().equals(resData.getApplicantCardNum())) {
                                BeanUtils.copyProperties(resData, data);
                                data.setIsQueryReturned(1);
                                data.setQueryReturnDt(DateUtils.getCurrentDate());
                                compareService.saveOrUpdateCivilOfficalData(data);
                                break;
                            }
                        }
                    }
                }
                // 更新比对记录表
                Map condition = new HashMap();
                condition.put("batchNum", civilResponse.getBatchNum());
                condition.put("type", civilResponse.getDepartmentNum());
                condition.put("mainApplicantIdNum", civilResponse.getMainApplicantCardId());
                List<CompareRecord> recordUpdateList = compareRecordService
                        .queryCompareRecordListByCondition(condition);
                if (recordUpdateList != null) {
                    for (CompareRecord record : recordUpdateList) {
                        record.setRecvNum(record.getRecvNum() + civilResponse.getMemberDatas().size());
                    }
                    compareRecordService.saveOrUpdate(recordUpdateList);
                }
            }
        }
        return true;
    }

    @Override
    public boolean sendHPResponse(List<HPCompareResponse> responseList) {
        if (responseList != null && !responseList.isEmpty()) {
            for (CompareResponse response : responseList) {
                // 更新委办返回表
                HPCompareResponse hpResponse = (HPCompareResponse) response;
                List<HPOfficalData> updateList = compareService.getHPOfficalDataForUpdate(hpResponse.getBatchNum(),
                        hpResponse.getDepartmentNum(), hpResponse.getMainApplicantCardId());
                if (updateList != null) {
                    for (HPOfficalData data : updateList) {
                        for (HPData resData : hpResponse.getMemberDatas()) {
                            if (data.getApplicantCardNum().equals(resData.getApplicantCardNum())) {
                                BeanUtils.copyProperties(resData, data);
                                data.setIsQueryReturned(1);
                                data.setQueryReturnDt(DateUtils.getCurrentDate());
                                compareService.saveOrUpdateHPOfficalData(data);
                                break;
                            }
                        }
                    }
                }
                // 更新比对记录表
                Map condition = new HashMap();
                condition.put("batchNum", hpResponse.getBatchNum());
                condition.put("type", hpResponse.getDepartmentNum());
                condition.put("mainApplicantIdNum", hpResponse.getMainApplicantCardId());
                List<CompareRecord> recordUpdateList = compareRecordService
                        .queryCompareRecordListByCondition(condition);
                if (recordUpdateList != null) {
                    for (CompareRecord record : recordUpdateList) {
                        record.setRecvNum(record.getRecvNum() + hpResponse.getMemberDatas().size());
                    }
                    compareRecordService.saveOrUpdate(recordUpdateList);
                }
            }
        }
        return true;
    }

    @Override
    public boolean sendIBAResponse(List<IBACompareResponse> responseList) {
        if (responseList != null && !responseList.isEmpty()) {
            for (CompareResponse response : responseList) {
                // 更新委办返回表
                IBACompareResponse ibaResponse = (IBACompareResponse) response;
                List<IBAOfficalData> updateList = compareService.getIBAOfficalDataForUpdate(ibaResponse.getBatchNum(),
                        ibaResponse.getDepartmentNum(), ibaResponse.getMainApplicantCardId());
                if (updateList != null) {
                    for (IBAOfficalData data : updateList) {
                        for (IBAData resData : ibaResponse.getMemberDatas()) {
                            if (data.getApplicantCardNum().equals(resData.getApplicantCardNum())) {
                                BeanUtils.copyProperties(resData, data);
                                data.setIsQueryReturned(1);
                                data.setQueryReturnDt(DateUtils.getCurrentDate());
                                compareService.saveOrUpdateIBAOfficalData(data);
                                break;
                            }
                        }
                    }
                }
                // 更新比对记录表
                Map condition = new HashMap();
                condition.put("batchNum", ibaResponse.getBatchNum());
                condition.put("type", ibaResponse.getDepartmentNum());
                condition.put("mainApplicantIdNum", ibaResponse.getMainApplicantCardId());
                List<CompareRecord> recordUpdateList = compareRecordService
                        .queryCompareRecordListByCondition(condition);
                if (recordUpdateList != null) {
                    for (CompareRecord record : recordUpdateList) {
                        record.setRecvNum(record.getRecvNum() + ibaResponse.getMemberDatas().size());
                    }
                    compareRecordService.saveOrUpdate(recordUpdateList);
                }
            }
        }
        return true;
    }

    @Override
    public boolean sendSIResponse(List<SICompareResponse> responseList) {
        if (responseList != null && !responseList.isEmpty()) {
            for (CompareResponse response : responseList) {
                // 更新委办返回表
                SICompareResponse siResponse = (SICompareResponse) response;
                List<SIOfficalData> updateList = compareService.getSIOfficalDataForUpdate(siResponse.getBatchNum(),
                        siResponse.getDepartmentNum(), siResponse.getMainApplicantCardId());
                if (updateList != null) {
                    for (SIOfficalData data : updateList) {
                        for (SIData resData : siResponse.getMemberDatas()) {
                            if (data.getApplicantCardNum().equals(resData.getApplicantCardNum())) {
                                BeanUtils.copyProperties(resData, data);
                                data.setIsQueryReturned(1);
                                data.setQueryReturnDt(DateUtils.getCurrentDate());
                                compareService.saveOrUpdateSIOfficalData(data);
                                break;
                            }
                        }
                    }
                }
                // 更新比对记录表
                Map condition = new HashMap();
                condition.put("batchNum", siResponse.getBatchNum());
                condition.put("type", siResponse.getDepartmentNum());
                condition.put("mainApplicantIdNum", siResponse.getMainApplicantCardId());
                List<CompareRecord> recordUpdateList = compareRecordService
                        .queryCompareRecordListByCondition(condition);
                if (recordUpdateList != null) {
                    for (CompareRecord record : recordUpdateList) {
                        record.setRecvNum(record.getRecvNum() + siResponse.getMemberDatas().size());
                    }
                    compareRecordService.saveOrUpdate(recordUpdateList);
                }
            }
        }
        return true;
    }

    @Override
    public boolean sendTaxResponse(List<TaxCompareResponse> responseList) {
        if (responseList != null && !responseList.isEmpty()) {
            for (CompareResponse response : responseList) {
                // 更新委办返回表
                TaxCompareResponse taxResponse = (TaxCompareResponse) response;
                List<TaxOfficalData> updateList = compareService.getTaxOfficalDataForUpdate(taxResponse.getBatchNum(),
                        taxResponse.getDepartmentNum(), taxResponse.getMainApplicantCardId());
                if (updateList != null) {
                    for (TaxOfficalData data : updateList) {
                        for (TaxData resData : taxResponse.getMemberDatas()) {
                            if (data.getApplicantCardNum().equals(resData.getApplicantCardNum())) {
                                BeanUtils.copyProperties(resData, data);
                                data.setIsQueryReturned(1);
                                data.setQueryReturnDt(DateUtils.getCurrentDate());
                                compareService.saveOrUpdateTaxOfficalData(data);
                                break;
                            }
                        }
                    }
                }
                // 更新比对记录表
                Map condition = new HashMap();
                condition.put("batchNum", taxResponse.getBatchNum());
                condition.put("type", taxResponse.getDepartmentNum());
                condition.put("mainApplicantIdNum", taxResponse.getMainApplicantCardId());
                List<CompareRecord> recordUpdateList = compareRecordService
                        .queryCompareRecordListByCondition(condition);
                if (recordUpdateList != null) {
                    for (CompareRecord record : recordUpdateList) {
                        record.setRecvNum(record.getRecvNum() + taxResponse.getMemberDatas().size());
                    }
                    compareRecordService.saveOrUpdate(recordUpdateList);
                }
            }
        }
        return true;
    }

    @Override
    public boolean sendPSResponse(List<PSCompareResponse> responseList) {
        if (responseList != null && !responseList.isEmpty()) {
            for (CompareResponse response : responseList) {
                // 更新委办返回表
                PSCompareResponse psResponse = (PSCompareResponse) response;
                List<PopulationOfficalData> updateList = compareService.getPSOfficalDataForUpdate(
                        psResponse.getBatchNum(), psResponse.getDepartmentNum(), psResponse.getMainApplicantCardId());
                if (updateList != null) {
                    for (PopulationOfficalData data : updateList) {
                        for (PSData resData : psResponse.getMemberDatas()) {
                            if (data.getApplicantCardNum().equals(resData.getApplicantCardNum())) {
                                BeanUtils.copyProperties(resData, data);
                                data.setIsQueryReturned(1);
                                data.setQueryReturnDt(DateUtils.getCurrentDate());
                                compareService.saveOrUpdatePSOfficalData(data);
                                break;
                            }
                        }
                    }
                }
                // 更新比对记录表
                Map condition = new HashMap();
                condition.put("batchNum", psResponse.getBatchNum());
                condition.put("type", psResponse.getDepartmentNum());
                condition.put("mainApplicantIdNum", psResponse.getMainApplicantCardId());
                List<CompareRecord> recordUpdateList = compareRecordService
                        .queryCompareRecordListByCondition(condition);
                if (recordUpdateList != null) {
                    for (CompareRecord record : recordUpdateList) {
                        record.setRecvNum(record.getRecvNum() + psResponse.getMemberDatas().size());
                    }
                    compareRecordService.saveOrUpdate(recordUpdateList);
                }
            }
        }
        return true;
    }
}
